﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_SanPham : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        
            string query;
            //string url;
            DataTable dt = new DataTable();
            XuLy xl = new XuLy();
          
            if (Request.QueryString["MaChatLieu"] != null)
            {
               
                query = "select * from SanPham where MaChatLieu='" + Request.QueryString["MaChatLieu"].ToString() + "'";
                
            }

           else if (Request.QueryString["MaKieu"] != null)
            {
                   
                query = "select * from SanPham where MaKieu='" + Request.QueryString["MaKieu"].ToString() + "'";

               
            }
            else if (Session["timkiem"]!=null)
            {
                
                query ="select * from SanPham where TenSP like'%"+Session["TenSP"].ToString()+"%'";
            
            }
            else if (Session["timkiemnangcao"] != null)
            {
                query = "select * from SanPham where giabansp>" + Session["giatu"] + " and giabansp<=" + Session["giaden"] + "";
            }
            else
            {
                //currentPage = Request.QueryString["MaKieu"];
                query = "select * from SanPham";
                //pageIndex = Convert.ToInt32(currentPage);
               
            }
            
         
            
            CollectionPager1.MaxPages = 100;
            CollectionPager1.PageSize = 6;
            CollectionPager1.DataSource = xl.Load(query).DefaultView;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();

            
        }
    }
    XuLy xl = new XuLy();
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ChonMua")
        {
            DataTable sanpham = new DataTable();
            DataTable giohang = new DataTable();
            sanpham = xl.Load("select * from SanPham where MaSP='" + e.CommandArgument.ToString() + "'");
            giohang = (DataTable)Session["GioHang"];
            giohang.DefaultView.Sort = "MaSP";
            int vt = giohang.DefaultView.Find(e.CommandArgument);
           // int vt = giohang.DefaultView.Find(e.CommandArgument);// lay vi tri cua san pham
            if (vt >= 0)
            {
                //xuat thong bao co san pham trong gio hang
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", "alert('" + giohang.Rows[vt]["SoLuong"].ToString() + "')", true);
                
                if (int.Parse(giohang.Rows[vt]["SoLuong"].ToString()) > 3)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", "alert('Sản Phẩm đã có trong giỏ hàng')", true);
                }
                else
                {
                    giohang.Rows[vt]["SoLuong"] = int.Parse(giohang.Rows[vt]["SoLuong"].ToString()) + 1;
                    giohang.Rows[vt]["ThanhTien"] = int.Parse(giohang.Rows[vt]["SoLuong"].ToString()) * int.Parse(giohang.Rows[vt]["Gia"].ToString());
                }
               
            }
            else
            {
                DataRow d;
                d = giohang.NewRow();
              //  d["TenDN"] = Session["DangNhap"].ToString();
                d["MaSP"] = e.CommandArgument.ToString();
                d["TenSp"] = sanpham.Rows[0]["TenSP"];
                d["Gia"] = sanpham.Rows[0]["GiaBanSP"];
                d["SoLuong"] = 1;
                d["ThanhTien"] = d["Gia"];
                d["NgayDat"] = DateTime.Now;
                giohang.Rows.Add(d);
            }
            giohang.AcceptChanges();
            Session["GioHang"] = giohang;
            Response.Redirect("~/TrangChu.aspx");
        }
    }
   
}